package com.platform.modules.app.controller;


import com.alibaba.fastjson.JSONObject;
import com.platform.annotation.LoginUser;
import com.platform.common.utils.Constant;
import com.platform.common.utils.RestResponse;
import com.platform.common.utils.StringUtils;
import com.platform.modules.mall.activity.entity.bo.*;
import com.platform.modules.mall.activity.service.GoShoppingService;
import com.platform.modules.mall.entity.MallOrderEntity;
import com.platform.modules.mall.entity.MallRuleEntity;
import com.platform.modules.mall.entity.MallUserEntity;
import com.platform.modules.mall.service.MallOrderService;
import com.platform.modules.mall.service.MallRuleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;


@Api(tags = "AppActivityGoShoppingController|活动购买商品")
@RestController
@RequestMapping("/app/activityGoShopping")
public class AppActivityGoShoppingController {

    @Autowired
    private GoShoppingService goShoppingService;
    @Autowired
    private MallRuleService mallRuleService;

    @PostMapping("shopping")
    @ApiOperation(value = "活动购买商品", notes = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "goodsId", value = "商品id",dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "number", value = "购买数量", required = true,dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "activityId", value = "活动id", required = true, dataType = "string")
    })
    public RestResponse shopping(@ApiIgnore ActivityGoshoppingBo bo,@LoginUser MallUserEntity userEntity) {

        if (StringUtils.isBlank(userEntity.getMobile())){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NO_PHONE);
        }
        bo.setPhone(userEntity.getMobile());
        bo.setUserId(userEntity.getId());
        bo.setUserName(userEntity.getUserName());
        ActivityOrderVo vo = goShoppingService.goShopping(bo);
        return RestResponse.success().put("data", vo);
    }

    @PostMapping("shopping2")
    @ApiOperation(value = "活动购买商品", notes = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "goodsId", value = "商品id",dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "number", value = "购买数量", required = true,dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "activityId", value = "活动id", required = true, dataType = "string")
    })
    public RestResponse shopping2(@ApiIgnore @RequestBody ActivityGoshoppingBo bo,@LoginUser MallUserEntity userEntity) {

        if (StringUtils.isBlank(userEntity.getMobile())){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NO_PHONE);
        }
        bo.setPhone(userEntity.getMobile());
        bo.setUserId(userEntity.getId());
        bo.setUserName(userEntity.getUserName());
        ActivityOrderVo vo = goShoppingService.goShopping(bo);
        return RestResponse.success().put("data", vo);
    }

    @GetMapping("activityInfo")
    @ApiOperation(value = "获取活动信息", notes = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "activityCode", value = "活动唯一标识", required = true, dataType = "string")
    })
    public RestResponse activityInfo(@ApiIgnore ActivityGoshoppingBo bo) {
        ActivityInfoVo vo = goShoppingService.activityInfo(bo);
        return RestResponse.success().put("data", vo);
    }

    /**
     * 获取活动数据，以及商品数据
     */
    @GetMapping("activityVerify")
    @ApiOperation(value = "验证活动资格", notes = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "id", value = "活动唯一标识", required = true, dataType = "string")
    })
    public RestResponse activityVerify(String id,@LoginUser MallUserEntity userEntity) {

        if (StringUtils.isBlank(userEntity.getMobile())){
            throw ActivityResultStatus.buildException(ActivityResultStatus.Status.NO_PHONE);
        }
        ActivityGoshoppingBo bo =new ActivityGoshoppingBo();
        bo.setActivityId(id);
        bo.setUserId(userEntity.getId());
        bo.setUserName(userEntity.getNickname());
        bo.setPhone(userEntity.getMobile());
        ActivityVerifyVo activityInfoVo = goShoppingService.activityVerify(bo);

        return RestResponse.success().put("data", activityInfoVo.getObj());
    }

    /**
     * 获取活动数据，以及商品数据
     */
    @GetMapping("activityRecord")
    @ApiOperation(value = "活动的中奖记录", notes = "中奖记录")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "activityId", value = "活动id", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "count", value = "查询条数", required = true, dataType = "Integer"),
    })
    public RestResponse activityRecord(@ApiIgnore ActivityGoshoppingBo bo) {
        List<ActivityRecordVo> page = goShoppingService.activityRecord(bo);
        return RestResponse.success().put("data", page);
    }


    @GetMapping("activityRule")
    @ApiOperation(value = "获取活动单独配置的规则", notes = "活动信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "用户token", required = true, dataType = "string"),
            @ApiImplicitParam(paramType = "query", name = "type", value = "活动唯一标识", required = true, dataType = "String")
    })
    public RestResponse activityRule(String type) {
        if (StringUtils.isBlank(type)){
            return RestResponse.error("types不能为空");
        }
        String[] typess = type.split(",");

        List<MallRuleEntity> list =new ArrayList<>(typess.length);
        for (String t:typess){
            MallRuleEntity entity = mallRuleService.getEntityByType(Integer.valueOf(t));
            if (entity==null||entity.getDetail()==null){continue;}

            entity.setDetail(entity.getDetail().replaceAll("<[^>]+>", ""));
            list.add(entity);
        }
        return RestResponse.success().put("data", list);
    }
}
